var Utils = require('geoUtils');

var AttachNodeType = cc.Enum({
    BONE: 0,
    SLOT: 1,
});

var AttachData = cc.Class({
    name: 'geoDragonNodeAttachData',
    properties: {
        type: {
            default: AttachNodeType.SLOT,
            type: cc.Enum(AttachNodeType),
        },
        name: 'name',
        nodeToAttach: {
            default: null,
            type: cc.Node,
            tooltip: '要粘贴在slot上的节点',
        },
        usePosition: true,
        useRotation: false,
        useScale: false,
    }
});

/**
 * 对龙骨的插槽中的图片进行显示操作
 */
var DragonNodeAttach = cc.Class({
    extends: cc.Component,

    properties: {
        armatureDisplay: {
            default: null,
            type: dragonBones.ArmatureDisplay
        },
        attachDatas: {
            default: [],
            type: [AttachData],
        },
        attachOnLoad: false
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.armatureDisplay = this.armatureDisplay || this.node.getComponent(dragonBones.ArmatureDisplay);
        if (this.attachOnLoad == true)
            this.doAttach();
    },

    doAttach() {
        this.attachDatas.forEach((e => {
            this.attachTo(e.type, e.name, e.nodeToAttach, e.usePosition, e.useRotation, e.useScale);
        }).bind(this));
    },
    /**
     * 将节点依附到槽上
     * @param {SlotType} type 骨骼或是插槽
     * @param {string} name 槽名称
     * @param {cc.Node} node 要依附在槽上的节点
     */
    attachTo(type, name, node, usePosition, useRotation, useScale) {
        var slot;
        if (type == AttachNodeType.BONE) {
            slot = this.armatureDisplay.armature().getBone(name);
        } else if (type == AttachNodeType.SLOT) {
            slot = this.armatureDisplay.armature().getSlot(name);
        }
        this._attachTo(slot, node, usePosition, useRotation, useScale);
    },
    _attachTo(attach, node, usePosition, useRotation, useScale) {
        // slot.displayIndex = displayIndex;
        this._attaches = this._attaches || [];
        this._attaches.push({
            attach: attach,
            node: node,
            usePosition: usePosition,
            useRotation: useRotation,
            useScale: useScale
        });
    },

    update(dt) {
        this._attaches && this._attaches.forEach(e => {
            var transform = e.attach.global;
            var node = e.node;
            if (e.usePosition === true) {
                var slotPos = new cc.Vec2(transform.x, -transform.y);// 这里y竟然是负值？可能龙骨坐标系和cocos不一样
                slotPos = Utils.node.getNodeToWorldPosition(this.armatureDisplay.node, slotPos);
                Utils.node.setWorldPosition(node, slotPos);
            }
            if (e.useRotation === true){
                var slotRot = cc.misc.radiansToDegrees(transform.rotation);
                slotRot = Utils.node.getNodeToWorldRotation(this.armatureDisplay.node, slotRot);
                Utils.node.setWorldRotation(node, -slotRot);// 这里也是负的
            }
            if (e.useScale === true) {
                var slotScale = new cc.Vec2(transform.scaleX, transform.scaleY);
                slotScale = Utils.node.getNodeToWorldScale(this.armatureDisplay.node, slotScale.x, slotScale.y);
                Utils.node.setWorldScale(node, slotScale);
            }
        });
    },
});

DragonNodeAttach.AttachNodeType = AttachNodeType;

module.exports = DragonNodeAttach;
